#ifndef __CONST_DEFINE_H__
#define __CONST_DEFINE_H__

//two server
#define MAX_SVR_NUM 2

//max connecting 
#define MAX_CONNECT_SIZE  2048

#define MAX_PKG_LEN (256L*256L*256L-1)

#define MEMORY_PIECE (1024L*16L)

#define MAX_SEND_BLOCK (1024L*1024L)

#define PKG_HEAD_LEN 4  

#define PKG_NUM_OFFSET 3

#define BASE_SECONDS 1160370232

#define MY_SQL_ERROR 0xFF

#define MAX_MYSQL_CMD 0xFF

//bottom error code
enum{
	ERR_RECV_BUFF_FULL = 0x01,
	ERR_SEND_BUFF_FULL = 0x02,
	ERR_PKG_TOO_LARGE = 0x03,
	ERR_CONN_STATUS	= 0x04,
	ERR_ALLOC_MEM = 0x05,
};

//ptr point to package head;
#define PKG_LEN(ptr)  (uint32_t)(((uint32_t)((uint8_t)(ptr)[0])) \
					 +(((uint32_t)((uint8_t)(ptr)[1]))<<8) \
					 +(((uint32_t)((uint8_t)(ptr)[2]))<<16))

//ptr point to package head;
#define CMD_NUM(ptr) ((static_cast<uint8_t>(*((ptr)+PKG_HEAD_LEN))>=MY_COM_END)?MY_COM_END:static_cast<uint8_t>(*((ptr)+PKG_HEAD_LEN)))

#define RESULT_SET_ERRNO(ptr) (uint32_t)(((uint32_t)((uint8_t)(ptr)[PKG_HEAD_LEN+1])) \
						 +(((uint32_t)((uint8_t)(ptr)[PKG_HEAD_LEN+2]))<<8))

#define ERR_UNSUPPORT_PKG(ptr, size, no) do{\
	*(ptr+4) = MY_SQL_ERROR; \
	*((uint16*)((ptr)+5))= (uint16)(ER_NOT_SUPPORTED_YET);\
	sprintf(ptr+7, "%s", "#42000sql dos not support(from maskd)."); \
	*(size) = (1+2+38); \
	*(ptr) = (uint8_t)((*(size))); \
	*(ptr+1) = (uint8_t)(((*(size)))>>8); \
	*(ptr+2) = (uint8_t)(((*(size)))>>16); \
	*(ptr+3) = (++no); \
	*(size) = (*(size) + 4); \
	}while(0);

#define ERR_UNREACH_PKG(ptr, size) do{\
	*(ptr+4) = MY_SQL_ERROR; \
	*((uint16*)((ptr)+5))= (uint16)(ER_ABORTING_CONNECTION);\
	sprintf(ptr+7, "%s", "connection error(from maskd)."); \
	*(size) = (1+2+35); \
	*(ptr) = (uint8_t)((*(size))); \
	*(ptr+1) = (uint8_t)(((*(size)))>>8); \
	*(ptr+2) = (uint8_t)(((*(size)))>>16); \
	*(ptr+3) = (0); \
	*(size) = (*(size) + 4); \
	}while(0);

#define ERR_ACCESS_DENY_PKG(ptr, size) do{\
	*(ptr+4) = MY_SQL_ERROR; \
	*((uint16*)((ptr)+5))= (uint16)(ER_ABORTING_CONNECTION);\
	sprintf(ptr+7, "%s", "access denied(from maskd)."); \
	*(size) = (1+2+32); \
	*(ptr) = (uint8_t)((*(size))); \
	*(ptr+1) = (uint8_t)(((*(size)))>>8); \
	*(ptr+2) = (uint8_t)(((*(size)))>>16); \
	*(ptr+3) = (0); \
	*(size) = (*(size) + 4); \
	}while(0);

#define ON_CONNECT_FROM(handle, connNum) do{ \
	((handle)->IsConnected()?(++connNum):(connNum)); \
	CONN("On%sConnect from %s:%d, Sockfd:%d, ConnNum = %d", \
		(handle)->IsConnected()?"":"Half", \
		(handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort(), \
		(handle)->GetConnHandle(), (connNum)); \
}while(0);

#define ON_CONNECT_TO(handle, connNum) do{ \
	((handle)->IsConnected()?(++connNum):(connNum)); \
	CONN("On%sConnect to %s:%d, Sockfd:%d, ConnNum:%d", \
		(handle)->IsConnected()?"":"Half", \
		(handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort(), \
		(handle)->GetConnHandle(), (connNum)); \
}while(0);

#define ON_CLOSE_FROM(handle, ret, connNum) do{ \
	(--connNum); \
	CONN("OnClose from %s:%d, RetCode:%d, Sockfd = %d, ConnNum = %d", \
		(handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort(), \
		(ret), (handle)->GetConnHandle(), (connNum)); \
}while(0);

#define ON_CLOSE_TO(handle, ret, connNum) do{ \
	((handle)->IsConnected()?(--connNum):(connNum)); \
	CONN("On%sClose to %s:%d, RetCode:%d, Sockfd = %d, ConnNum = %d", \
		(handle)->IsConnected()?"":"Half", \
		(handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort(), \
		(ret), (handle)->GetConnHandle(), (connNum)); \
}while(0);

#define ON_ERROR_CONN_TO(ip, port, msg) do{ \
	CONN_ERROR("OnConnect to Mysql %s:%d Error, Msg:%s", \
		(ip), (port), (msg)); \
}while(0);

#define ON_SEND_TO(name, handle, size, ret) do{ \
	CONN("OnSend %s to %s:%d, Sockfd = %d, BufLen = %u, RetCode = %d", \
		(name), (handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort() \
		, (handle)->GetConnHandle(), (size), (ret)); \
}while(0);

#define ON_SEND_ERROR_TO(name, handle, size, ret) do{ \
	CONN_ERROR("OnSend %s to %s:%d, Sockfd = %d, BufLen = %u, RetCode = %d", \
		(name), (handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort() \
		, (handle)->GetConnHandle(), (size), (ret)); \
}while(0);


#define ON_ERROR_SEND_TO(name, handle, size, ret) do{ \
	CONN_ERROR("OnSend %s to %s:%d Error, Sockfd = %d, BufLen = %u, RetCode = %d", \
		(name), (handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort() \
		, (handle)->GetConnHandle(), (size), (ret)); \
}while(0);

#define ON_RECV_FROM(handle, size) do{ \
	CONN("OnRecv from %s:%d, Sockfd = %d, BufLen = %u", \
	(handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort(), \
	(handle)->GetConnHandle(), (size)); \
}while(0);

#define ON_RECV_BREAK_FROM(handle) do{ \
	CONN("OnRecvBreakPkg from %s:%d, Sockfd = %d", \
	(handle)->GetPeerHostIP().c_str(), (handle)->GetPeerHostPort(), \
	(handle)->GetConnHandle()); \
}while(0);

#endif
